package kotan.view;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import kotan.Kotan;
import kotan.datastore.api.Key;
import kotan.model.EntityModel;

public class NavigationPanel extends JPanel {

    private static final long serialVersionUID = 1L;
    private JTextField inputField;
    
    public NavigationPanel() {
        initComponents();
    }

    private void initComponents() {
        JLabel label = new JLabel();
        label.setText("Input a kind name and load. ");
        inputField = new JTextField(20);
        JButton loadButton = new JButton();
        loadButton.setText("Load");
        loadButton.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent e) {
                onLoad();
            }
        });
        JButton saveButton = new JButton();
        saveButton.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent e) {
                onSave();
            }
        });
        saveButton.setText("Save");
        // Layout
        this.setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.weightx = 0.0;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.fill = GridBagConstraints.NONE;
        gbc.insets = new Insets(0, 2, 0, 5);
        add(label, gbc);
        gbc.gridx = 1;
        gbc.weightx = 0.0;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.fill = GridBagConstraints.NONE;
        gbc.insets = new Insets(0, 2, 0, 5);
        add(inputField, gbc);
        gbc.gridx = 2;
        gbc.weightx = 0.0;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.fill = GridBagConstraints.NONE;
        gbc.insets = new Insets(0, 2, 0, 5);
        add(loadButton, gbc);
        gbc.gridx = 3;
        gbc.weightx = 0.6;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.fill = GridBagConstraints.NONE;
        gbc.insets = new Insets(0, 10, 0, 5);
        add(saveButton, gbc);
    }


    protected void onLoad() {
        String kind = inputField.getText().trim();
        Kotan.get().getService().load(kind);
    }

    protected void onSave() {
        List<EntityModel> updateList = Kotan.get().getDatastore().getAllUpdateEntities();
        List<Key> deleteList = Kotan.get().getDatastore().getAllDeleteEntities();
        if (updateList.isEmpty() && deleteList.isEmpty()) return;
        int result = JOptionPane.showConfirmDialog(this, "Save all entities. Are you sure?", "Save Confirm", JOptionPane.OK_CANCEL_OPTION);
        if (result != JOptionPane.OK_OPTION) return;
        Kotan.get().getService().uploadAndDelete(updateList, deleteList);
    }
}
